<script setup lang="ts">
import { getMedicalOrderLogisticsDetail } from '@/services/order'
import type { Logistics } from '@/types/order'
import { onMounted } from 'vue'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()
import startIcon from '@/assets/start.png'
import endIcon from '@/assets/end.png'
import carIcon from '@/assets/car.png'
import type { Location } from '@/types/order'
import AMapLoader from '@amap/amap-jsapi-loader'
window._AMapSecurityConfig = {
  securityJsCode: '415e917da833efcf2d5b69f4d821784b'
}

// 物流信息
const logistics = ref<Logistics>()
onMounted(async () => {
  const res = await getMedicalOrderLogisticsDetail(route.params.id as string)
  logistics.value = res.data

  initMap()
})

// 初始化地图
const initMap = () => {
  AMapLoader.load({
    key: '4eed3d61125c8b9c168fc22414aaef7e',
    version: '2.0'
  }).then((AMap) => {
    const map = new AMap.Map('map', {
      mapStyle: 'amap://styles/whitesmoke',
      zoom: 12 //设置地图的缩放级别
    })

    // 自动规划路径
    AMap.plugin('AMap.Driving', function () {
      const driving = new AMap.Driving({
        map,
        showTraffic: false, // 不展示道路拥挤信息
        hideMarkers: true // 隐藏标记
      })
      if (
        logistics.value?.logisticsInfo &&
        logistics.value?.logisticsInfo.length >= 2
      ) {
        const list = [...logistics.value.logisticsInfo]
        console.log(list)
        // 提取创建标记函数
        const getMarker = (
          point: Location,
          image: string,
          width = 25,
          height = 30
        ) => {
          // 绘制起点标记
          // (1) 创建icon实例
          const icon = new AMap.Icon({
            size: new AMap.Size(width, height), // 尺寸
            image, // Icon
            imageSize: new AMap.Size(width, height) // 尺寸
          })
          // (2) 将实例添加到标记上
          const marker = new AMap.Marker({
            position: new AMap.LngLat(point?.longitude, point?.latitude), // 经纬度
            offset: new AMap.Pixel(-width / 2, -height), // width 一半  height 完整
            icon // 指定实例
          })
          return marker
        }

        // 起点
        const start = list.shift()
        const startMarker = getMarker(start!, startIcon)
        // 将标记添加到地图上
        map.add(startMarker)

        // 终点
        const end = list.pop()
        const endMarker = getMarker(end!, endIcon)
        map.add(endMarker)

        driving.search(
          [start?.longitude, start?.latitude],
          [end?.longitude, end?.latitude],
          {
            waypoints: list.map((item) => [item.longitude, item.latitude])
          },
          function () {
            // 绘制当前运输点标记
            const curr = logistics.value?.currentLocationInfo
            const currMarker = getMarker(curr!, carIcon, 50, 30)
            map.add(currMarker)

            // 3s后定位到当前的运输位置
            setTimeout(() => {
              map.setFitView([currMarker])
              map.setZoom(10)
            }, 3000)
          }
        )
      }
    })
  })
}
</script>

<template>
  <div class="order-logistics-page" v-if="logistics">
    <div id="map">
      <div class="title">
        <van-icon name="arrow-left" @click="$router.back()" />
        <span>{{ logistics.statusValue }}</span>
        <van-icon name="service" />
      </div>
      <div class="current">
        <p class="status">
          {{ logistics.statusValue }} {{ logistics.estimatedTime }}
        </p>
        <p class="predict">
          <span>{{ logistics.name }}</span>
          <span>{{ logistics.awbNo }}</span>
        </p>
      </div>
    </div>
    <div class="logistics">
      <p class="title">物流详情</p>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="item in logistics.list" :key="item.id">
          <p class="status">{{ item.statusValue }}</p>
          <p class="content">
            {{ item.content }}
          </p>
          <p class="time">{{ item.createTime }}</p>
        </van-step>
      </van-steps>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-logistics-page {
  --van-step-icon-size: 18px;
  --van-step-circle-size: 10px;
}
#map {
  height: 450px;
  background-color: var(--cp-bg);
  overflow: hidden;
  position: relative;
  .title {
    background-color: #fff;
    height: 46px;
    width: 355px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 16px;
    position: absolute;
    left: 10px;
    top: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    > span {
      flex: 1;
      text-align: center;
    }
    .van-icon {
      font-size: 18px;
      color: #666;
      &:last-child {
        color: var(--cp-primary);
      }
    }
  }
  .current {
    height: 80px;
    border-radius: 4px;
    background-color: #fff;
    height: 80px;
    width: 355px;
    box-sizing: border-box;
    padding: 15px;
    position: absolute;
    left: 10px;
    bottom: 10px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    .status {
      font-size: 15px;
      line-height: 26px;
    }
    .predict {
      color: var(--cp-tip);
      font-size: 13px;
      margin-top: 5px;
      > span {
        padding-right: 10px;
      }
    }
  }
}
.logistics {
  padding: 0 10px 20px;
  .title {
    font-size: 16px;
    padding: 15px 5px 5px;
  }
  .van-steps {
    :deep(.van-step) {
      &::after {
        display: none;
      }
    }
    .status {
      font-size: 15px;
      color: var(--cp-text3);
      margin-bottom: 4px;
    }
    .content {
      font-size: 13px;
      color: var(--cp-tip);
      margin-bottom: 4px;
    }
    .time {
      font-size: 13px;
      color: var(--cp-tag);
    }
  }
}
</style>
